package cn.itrip.order.service;

import cn.itrip.dao.ItripCommentMapper;
import cn.itrip.dao.ItripHotelOrderMapper;
import cn.itrip.dao.ItripImageMapper;
import cn.itrip.pojo.ItripComment;
import cn.itrip.pojo.ItripImage;
import cn.itrip.utlis.Page;
import cn.itrip.vo.comment.ItripListCommentVO;
import cn.itrip.vo.comment.ItripScoreCommentVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2018/8/26.
 * 评论业务处理
 */
@Service
public class CommentServiceImpl implements  CommentService {

    @Resource
    private ItripCommentMapper itripCommentMapper;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private ItripImageMapper itripImageMapper;

    /**
     * 保存评论信息
     * @param itripComment
     */
    @Override
    public Boolean saveComment(ItripComment itripComment, List<ItripImage> itripImageList) throws Exception {

            //计算总平均分
            int sum = itripComment.getFacilitiesscore()+itripComment.getHygienescore()+itripComment.getPositionscore()+itripComment.getServicescore();
            float score = (float) (sum/4.0);
            itripComment.setScore(Math.round(score));

            //保持成功后修改订单状态

            if(itripCommentMapper.insertSelective(itripComment)>0){
                itripHotelOrderMapper.updateByStatus(itripComment.getOrderid(),4);
                if(itripImageList!=null) {
                    //评论增加成功后 获取图片相应的关联Id 并保存
                    for (ItripImage itripImage : itripImageList) {
                        itripImage.setTargetid(itripComment.getId());
                        itripImageMapper.insertImages(itripImage);
                    }
                }
            }
            return true;
    }
    //根据酒店id 查询评论信息
    @Override
    public ItripScoreCommentVO getScoreCommentVo(Long hotelId) throws Exception {
       if(hotelId == null){
           throw new Exception("参数错误！");
       }
        return itripCommentMapper.selectByGrade(hotelId);

    }

    /**
     * 根据条件查询评论数量
     * @param map
     * @return
     */
    @Override
    public Integer selectByHotel(Map<String, Object> map) throws Exception {
        if(map.get("hotelId") != null) {
            return itripCommentMapper.selectByMap(map);
        }
        throw new Exception("酒店id 不能为空！");
    }

    /**
     * 根据条件 分页显示评论 起始页，页容量由前端指定
     * @param map
     * @param pageSize
     * @param pageNo
     * @return
     * @throws Exception
     */
    @Override
    public Page selectByPage(Map map, Integer pageSize, Integer pageNo) throws Exception {
        //查询总记录数
        Integer count = itripCommentMapper.selectByMap(map);
        //创建page 类 计算相应的数值
        Page page = new Page(pageNo,pageSize,count);
        map.put("beginPos",page.getBeginPos());
        map.put("pageSize",page.getPageSize());
        List<ItripListCommentVO> listCommentVOS = itripCommentMapper.selectByCommentVoList(map);
        page.setRows(listCommentVOS);
        return page;
    }
}
